<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/1/31
 * Time: 14:35
 */

namespace app\api\service;


class tongdunServiceArr
{
    private $arr;
    private $new_arr = array();
    # 用来处理第二阶段同盾数组的类
    public function __construct($arr)
    {
        $this->arr = $arr;
    }
    public function ConditionFun()
    {
        $this->execute_court();
        $this->discredit_count();
        $this->social_software();
        $this->risk();
        $this->other();
        return $this->new_arr;
    }

    /**
     *  对命中法院法院失信黑名单数组进行处理
     */
    private function execute_court()
    {
        $execute_court_arr = [];
        $execute_court = $this->arr['execute_court'];
        if(empty($execute_court) == false){
            foreach ($execute_court as $value){
                if(array_key_exists('case_date', $value) && array_key_exists('fraud_type_display_name', $value)){
                    $arr['time'] = $value['case_date'];
                    $arr['value'] = $value['fraud_type_display_name'];
                    array_push($execute_court_arr, $arr);
                }
            }
            # 身份证是否命中法院实现黑名单
            $this->new_arr['execute_court'] = $execute_court_arr;
            $this->new_arr['phone_black_list'] = true;
        }else{
            $this->new_arr['execute_court'] = false;
            $this->new_arr['phone_black_list'] = false;
        }
    }
    /**
     *  对手机网贷逾期风险检测数组进行处理
     */
    private function discredit_count()
    {
        $discredit_count = $this->arr['discredit_count'];
        $discredit_count_arr = [];
        if(empty($discredit_count) == false){
            $date = ['(0,30]', '(30,60]','(60,90]', '(90,180]'];
            # 逾期金额
            $all_overdue_amount = null;
            # 逾期次数
            $all_overdue_count = null;
            # 统计循环次数
            $num = 0;
            foreach ($discredit_count as $value){
                if(array_key_exists('overdue_day',$value) && array_key_exists('overdue_amount',$value)){
                    if(in_array($value['overdue_day'], $date) == true){
                        $overdue_amount = $this->overdue_amount($value['overdue_amount']);
                        if(is_array($overdue_amount)){
                            $overdue_amount = ($overdue_amount[0] + $overdue_amount[1])/2;
                        }
                        $all_overdue_amount +=$overdue_amount;
                        $all_overdue_count += $value['overdue_count'];
                        # 往数组插入逾期详情

                        $arr['time'] = $value['overdue_time'];
                        $overdue_amount_value = $this->overdue_amount($value['overdue_amount']);
                        if(is_array($overdue_amount_value)){
                            $overdue_amount_value = "$overdue_amount_value[0]-$overdue_amount_value[1]";
                        }
                        $arr['value'] = $overdue_amount_value;
                        array_push($discredit_count_arr, $arr);
                        $num ++;
                    }
                }
            }
            # 六个月内逾期次数
            $this->new_arr['discredit_count'] = $discredit_count_arr;
            # 六个月内借贷记录次数
            if($num == 0){
                $num = 1;
            }
            $this->new_arr['number_of_loans'] = $all_overdue_count;
            $average_arr['money'] = ceil($all_overdue_amount/$num);
            $average_arr['num'] = ceil($all_overdue_count/$num);
            $this->new_arr['average_execute_court'] = $average_arr;
        }else{
            $this->new_arr['discredit_count'] = null;
            $this->new_arr['number_of_loans'] = 0;
            $this->new_arr['average_execute_court'] = null;
        }
    }

    /**
     * 社交软件风险等级
     */
    private function social_software()
    {
        $social_software = $this->arr['grey_list'];
        $social_software_arr = [];
        if(empty($social_software) == false){
            foreach ($social_software as $value)
            if(array_key_exists('risk_level_display_name',$value) && array_key_exists('fraud_type_display_name',$value)){
                array_push($social_software_arr, $value['fraud_type_display_name']."(".$value['risk_level_display_name']."风险)");
            }
            $this->new_arr['social_software']  = $social_software_arr;
        }else{
           $this->new_arr['social_software']  = null;
        }
    }

    /**
     *  操作行为检测和身份证命中检测
     */
    private function risk()
    {
        $risk = $this->arr['risk'];
        $operational_behavior = [];
        $risk_id_number = config('tongdun.risk_id_number');
        if(empty($risk) == false){
            foreach ($risk as $value){
                if($value['parentUuid'] == ""){
                    array_push($operational_behavior, $value['name']);
                }else{
                    $id_number_value = $value['name'].': 否';
                    $key = $this->risk_id_number($id_number_value);
                    if($key !== false){
                        $risk_id_number[$key] = $value['name'].': 是';
                    }
                }
            }
            $this->new_arr['operational_behavior'] = $operational_behavior;
            $this->new_arr['online_loan_risk'] = $risk_id_number;
        }else{
            $this->new_arr['operational_behavior'] = $operational_behavior;
            $this->new_arr['online_loan_risk'] = $risk_id_number;
        }
    }

    /**
     * 自定义处理逾期金额的函数（(10000,50000] ， 50000+）
     */
    private function overdue_amount($data)
    {
        if(substr($data, -1) == '+'){
            return substr($data, 0, -1);
        }elseif (substr($data, -1) == ']'){
            $str = substr($data, 0,-1);
            $amount_str = substr($str, 1);
            $arr = explode(',', $amount_str);
            return $arr;
        }
    }

    private function other()
    {
        $this->new_arr['personal'] = $this->arr['personal'];
        $this->new_arr['hit_rule'] = $this->arr['hit_rule'];
        $cross_partner = $this->arr['cross_partner'];
        if(empty($cross_partner) == true){
            $this->new_arr['cross_partner'] = null;
        }else{
            $this->new_arr['cross_partner'] = $cross_partner;
        }
    }

    private function risk_id_number($value)
    {
        $arr = config('tongdun.risk_id_number');
        return array_search($value, $arr);
    }
}